/*
 * @Author: oreky
 * @Date: 2020-12-24 10:28:55
 * @LastEditors: oreky
 * @LastEditTime: 2020-12-24 11:32:55
 * @Description:地理工具
 */

const EARTH_RADIUS = 6371229;

/**
 * 角度转换弧度
 * @param degree 角度
 */
export function degreeToRadian(degree: number): number {
  return (Math.PI * degree) / 180;
}

/**
 * 弧度转换角度
 * @param radian 弧度
 */
export function radianToDegree(radian: number): number {
  return (radian * 180) / Math.PI;
}

/**
 *
 * @param source 原点经纬度, [经度, 纬度]
 * @param distance 目标点距离远点长度, 单位:米
 * @param offset 目标点相对于原点的角度, 正北为0度
 */
export function coordinateByDistanceAndOffset(
  source: number[],
  distance: number,
  offset: number,
): number[] {
  const angle = 90 - offset;
  return [
    source[0] +
      (distance * Math.cos(degreeToRadian(angle))) /
        ((Math.PI * EARTH_RADIUS) / 180),
    source[1] +
      (distance * Math.sin(degreeToRadian(angle))) /
        ((Math.PI * EARTH_RADIUS) / 180),
  ];
}
